package top.mable.payment.schedule;


import lombok.extern.slf4j.Slf4j;
import org.redisson.RedissonLock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;


/**
 * 测试的job
 *
 * Scheduled ==>需要开启 @EnableScheduling //开启定时任务
 */

@Slf4j
@Component

public class DemoJob {

    @Autowired
    RedissonClient redissonClient;

    public static String DEMO_KEY = "DEMO_KEY";

    //@Scheduled(cron = "0/10 * * * * ?")
    public void doSomeThing(){

        Long start = System.currentTimeMillis();

        RLock lock = redissonClient.getLock(DEMO_KEY);
        System.out.println(lock.getName());
        try {
            lock.lock(5000, TimeUnit.MILLISECONDS);

            log.info("testLock:1:" + lock.toString()                                   // lock锁对象地址

                    + ",interrupted:" + Thread.currentThread().isInterrupted()        // 当前线程是否中断

                    + ",hold:" + lock.isHeldByCurrentThread()                            // 当前请求的线程是否是锁对象的持有者

                    + ",threadId:" + Thread.currentThread().getId()                     // 线程ID

                    + ",redissonClient:{}" + redissonClient)                                // redissonClient地址
;
            System.out.println("每隔5s执行一次");
        } finally {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
        log.info("时间过了{}",System.currentTimeMillis() - start);
    }

}
